#include "myactivation.h"
#include<cmath>


double Relu(const double& temp)
{
	if (temp > 0.0)
		return temp;
	else
		return 0.0;
}

double Reluderiv(const double& temp)
{
	if (temp >= 0.0)
		return 1.0;
	else
		return 0.0;
}

mat Relu_mat(const mat& temp)
{
	int r = temp.rows(), c = temp.cols();
	mat result(r, c);
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			result(i, j) = Relu(temp(i, j));
		}
	}
	return result;
}

mat Reluderiv_mat(const mat& temp)
{
	int r = temp.rows(), c = temp.cols();
	mat result(r, c);
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			result(i, j) = Reluderiv(temp(i, j));
		}
	}
	return result;
}




double Sigmoid(const double& temp)
{
	return 1.0/(1.0+exp(-temp));
}

double Sigmoidderiv(const double& temp)
{
	return Sigmoid(temp) * (1 - Sigmoid(temp));
}

mat Sigmoid_mat(const mat& temp)
{
	int r = temp.rows(), c = temp.cols();
	mat result(r, c);
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			result(i, j) = Sigmoid(temp(i, j));
		}
	}
	return result;
}

mat Sigmoidderiv_mat(const mat& temp)
{
	int r = temp.rows(), c = temp.cols();
	mat result(r, c);
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			result(i, j) = temp(i, j) * (1 - temp(i, j));
		}
	}
	return result;
}


double Tanh(const double& temp)
{
	return (exp(temp) - exp(-temp)) / (exp(temp) + exp(-temp));
}

double Tanhderiv(const double& temp)
{
	return 1.0 - pow(Tanh(temp),2);
}

mat Tanh_mat(const mat& temp)
{
	return temp.array().tanh();
}

mat Tanhderiv_mat(const mat& output)
{
	return -(output.cwiseProduct(output)).array() + 1;
}



mat Softmax(const mat& temp)
{
	int r = temp.rows(), c = temp.cols();
	mat result(r, c);
	for (int i = 0; i < r; i++)
	{
		double sumi = 0.0;
		for (int j = 0; j < c; j++)
		{
			result(i, j) = exp(temp(i, j));
			sumi += result(i, j);
		}
		for (int j = 0; j < c; j++)
		{
			result(i, j) = result(i, j) / sumi;
		}
	}
	return result;
}

mat Softmaxderiv(const mat& one_hot, const mat& pred)
{
	mat result(one_hot.rows(), one_hot.cols());
	result = pred - one_hot;
	return result;
}











